package com.jeff.controller;

import com.jeff.common.BaseInfo;
import com.jeff.common.mConst;
import com.jeff.po.FormData;
import com.jeff.service.FormDataService;
import com.jeff.util.ResultMap;
import com.jeff.vo.FormDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("formData")
public class FormDataController {

    @Autowired
    public FormDataService formDataService;

    @RequestMapping("batchData")
    @ResponseBody
    public ResultMap batchData(HttpServletRequest request,@RequestBody FormDataVo formDataVo) {
        ResultMap rm = new ResultMap();
        try {
            formDataVo.setCreateIp(BaseInfo.GetIp(request));
            formDataVo.setCreateId(BaseInfo.GetUserId(request));
            formDataService.batchData(formDataVo.getElements());
            rm.success().info("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("提交失败"+e.getMessage());
        }
        return rm;
    }

    @RequestMapping("list")
    @ResponseBody
    public ResultMap list() {
        ResultMap rm = new ResultMap();
        try {
            FormData formData = new FormData();
            formData.setId(null);
            formData.setState(mConst.STATE_T);
            List<FormData> list = formDataService.list(formData);
            rm.success().data(list);
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("fail");
        }
        return rm;
    }


    @RequestMapping("page")
    @ResponseBody
    public ResultMap page(String formId) {
        ResultMap rm = new ResultMap();
        try {
            rm.success().page(formDataService.summaryByFormIdByPage(formId));
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("获取分页失败"+e.getMessage());
        }
        return rm;
    }


    @RequestMapping("update")
    @ResponseBody
    public ResultMap update(FormData formData) {
        ResultMap rm = new ResultMap();
        try {
            formDataService.update(formData);
            rm.success().info("修改成功");
        } catch (Exception e) {
            rm.fail().info("修改失败");
        }
        return rm;
    }

    @RequestMapping("getDetail")
    @ResponseBody
    public ResultMap getDetailByLogId(String id) {
        ResultMap rm = new ResultMap();
        try {
            rm.success().data(formDataService.getFormDetail(id));
        } catch (Exception e) {
            rm.fail().info("error"+e.getMessage());
        }
        return rm;
    }

    @RequestMapping("remove")
    @ResponseBody
    public ResultMap remove(FormData formData) {
        ResultMap rm = new ResultMap();
        try {
            formData.setState(mConst.STATE_F);
            formDataService.update(formData);
            rm.success().info("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            rm.fail().info("删除失败");
        }
        return rm;
    }


}
